/**
 * Created by Administrator on 2018/11/5.
 */

/**
 * 页面主列表DataTable
 */
$(window).on('load', function() {
    $('#edit_operation_end_time_div input').datepicker({
        format: "yyyy-mm-dd",
        todayBtn: "linked",
        autoclose: true,
        todayHighlight: true
    });

    var data_table_init_1 = data_table_init;
    data_table_init_1.ajax = {
        url: 'projectQuery',
        dataSrc: 'data',
    };
    data_table_init_1.columnDefs = [{
        "targets": [0,1,2,3,7],
        "orderable": false
    }];
    data_table_init_1.columns = [
        {"data": "checkbox"},
        {"data": "title"},
        {"data": "name"},
        {"data": "phone"},
        {"data": "status"},
        {"data": "add_time"},
        {"data": "operation_end_time"},
        {"data": "action"}
    ];
    data_table_init_1.order = [5, 'desc'];
    var one_table = $('#one_table').DataTable(data_table_init_1);

    $('#table_list_toolbar').appendTo($("div.toolbar"));

    $('#search_name').on( 'change', function () {
        one_table
            .columns( 0 )
            .search( this.value )
            .draw();
    });
});

/**************************/

/**
 * 输入验证
 */
$().ready(function() {
    var e = "<i class='fa fa-times-circle'></i> ";
    //添加管理员
    $("#add_form").validate({
        rules: {
            add_title: {
                required: !0
            },
            add_customer: {
                required: !0
            }
        },
        messages: {
            add_title: {
                required: e + "请输入项目标题"
            },
            add_customer: {
                required: e + "请选择客户"
            }
        },
        submitHandler:function(form){
            add_ajax();
        }
    });

    //编辑管理员
    $("#edit_form").validate({
        rules: {
            edit_title: {
                required: !0
            },
            edit_customer: {
                required: !0
            }
        },
        messages: {
            edit_title: {
                required: e + "请输入项目名称"
            },
            edit_customer: {
                required: e + "请选择客户 "
            }
        },
        submitHandler:function(form){
            edit_ajax();
        }
    });

    //添加备注
    $("#project_add_remark_form").validate({
        rules: {
            project_next_title: {
                required: !0
            }
        },
        messages: {
            project_next_title: {
                required: e + "请输入备注信息"
            }
        },
        submitHandler:function(form){
            project_add_remark_ajax();
        }
    });
});

/**************************/

/**
 * 添加方法
 */
function add_ajax(){
    $("#add_submit").attr("disabled","disabled");
    var ajax_url = "addProjectAction";
    var query = $('#add_form').serialize();
    $.ajax({
        url:ajax_url,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            switch(data.status){
                case 200:
                    $("#add_window").modal("hide");
                    system_right_tip_set("",data.message,1);
                    $('#one_table').DataTable().draw();
                    break;
                case 404:
                    system_tip_set(data.message);
                    break;
            }
            $("#add_submit").removeAttr("disabled");
        }
    });
}

/**************************/

/**
 * 获取数据方法
 */
function get_data_ajax(data_id){
    var ajaxurl = "getProjectData";
    var query = new Object();
    query.data_id = data_id;
    $.ajax({
        url:ajaxurl,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            if(data.status == 200){
                $("#edit_title").val(data.content['title']);
                $("#edit_status").val(data.content['status']);
                $("#edit_operation_end_time").val(data.content['operation_end_time']);
                $("#edit_customer").val(data.content['customer_id']);

                $("input[name='data_id']").val(data_id);

                $("#edit_window").modal("show");
            }else{
                system_tip_set(data.message);
            }
        }
    });
}

/**
 * 编辑方法
 */
function edit_ajax(){
    $("#edit_submit").attr("disabled","disabled");
    var ajaxurl = "editProjectAction";
    var query = $('#edit_form').serialize();
    $.ajax({
        url:ajaxurl,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            switch(data.status){
                case 200:
                    $("#edit_window").modal("hide");
                    system_right_tip_set("",data.message,1);
                    $('#one_table').DataTable().draw();
                    break;
                case 404:
                    system_tip_set(data.message);
                    break;
            }
            $("#edit_submit").removeAttr("disabled");
        }
    });
}

/**************************/

/**
 * 批量删除
 */
function delete_ajax() {
    $("#delete_submit").attr("disabled","disabled");

    var id_str = "";
    var asd = $("input:checkbox[name='check_box[]']:checked").each(function(index, element) {
        if(id_str == ""){
            id_str = $(this).val();
        }else{
            id_str += "," + $(this).val();
        }
    });

    if(id_str == ""){
        $("#delete_submit").removeAttr("disabled");
        system_tip_set("请选择要删除的数据！");
        return false;
    }

    var ajaxurl = "deleteProjectAction";
    var query = new Object();
    query.id_str = id_str;
    $.ajax({
        url:ajaxurl,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            switch(data.status){
                case 200:
                    $("#delete_window").modal("hide");
                    system_right_tip_set("",data.message,1);
                    $('#one_table').DataTable().draw();
                    break;
                case 404:
                    system_tip_set(data.message);
                    break;
            }
            $("#delete_submit").removeAttr("disabled");
        }
    });
}

/**************************/

/**
 * 获取详情窗口数据
 * @param data_id
 */
function show_info_window(data_id){
    var ajax_url = "getProjectInfoAction";
    var query = new Object();
    query.data_id = data_id;
    $.ajax({
        url:ajax_url,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            switch(data.status){
                case 200:
                    $("#info_title").val(data.content["title"]);
                    $("#info_customer_name").val(data.content["name"]);
                    $("#info_customer_phone").val(data.content["phone"]);
                    $("#info_status").val(data.content["status"]);

                    $('input[name="data_id"]').val(data_id);
                    $("#info_window").modal("show");
                    break;
                case 404:
                    system_tip_set(data.msg);
                    break;
            }
        }
    });
    upload_two_table(data_id);
}

/**************************/

//显示添加备注窗口
function show_project_add_remark_window(){
    $("#info_window").modal("hide");
    var project_add_remark_window = $('#project_add_remark_window');
    project_add_remark_window.on('hidden.bs.modal', function () {
        $("#info_window").modal("show");
    });
    project_add_remark_window.modal("show");
}

//添加备注方法
function project_add_remark_ajax(){
    $("#project_add_remark_submit").attr("disabled","disabled");
    var ajax_url = "addRemarkAction";
    var query = $('#project_add_remark_form').serialize();
    $.ajax({
        url:ajax_url,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            switch(data.status){
                case 200:
                    system_right_tip_set("",data.message,1);
                    upload_two_table($('input[name="data_id"]').val());
                    $("#info_window").modal("show");
                    break;
                case 404:
                    system_right_tip_set("",data.message,0);
                    break;
            }
            $("#project_add_remark_window").modal("hide");
            $("#project_add_remark_submit").removeAttr("disabled");
        }
    });
}

/**************************/
//显示进入下一阶段窗口
function show_project_next_window(){
    var ajax_url = "getProjectNextStatusData";
    var query = $('#project_next_form').serialize();
    $.ajax({
        url:ajax_url,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            switch(data.status){
                case 200:
                    $("#info_window").modal("hide");
                    var project_next_window = $('#project_next_window');
                    project_next_window.on('hidden.bs.modal', function () {
                        $("#info_window").modal("show");
                    });
                    $("#project_next_title").html(data.content.message);
                    project_next_window.modal("show");
                    break;
                case 404:
                    system_right_tip_set("",data.message,0);
                    break;
            }
        }
    });
}

//进入下一阶段方法
function project_next_ajax(){
    $("#project_next_submit").attr("disabled","disabled");
    var ajax_url = "nextStatusAction";
    var query = $('#project_next_form').serialize();
    $.ajax({
        url:ajax_url,
        data:query,
        type:"Post",
        dataType:"json",
        success:function(data){
            switch(data.status){
                case 200:
                    system_right_tip_set("",data.message,1);
                    var data_id = $('input[name="data_id"]').val();
                    show_info_window(data_id);
                    upload_two_table(data_id);
                    break;
                case 404:
                    system_right_tip_set("",data.message,0);
                    break;
            }
            $("#project_next_window").modal("hide");
            $("#project_next_submit").removeAttr("disabled");
        }
    });
}
/**************************/

/**
 * 刷新详情窗口列表
 * @param project_id
 */
function upload_two_table(project_id){
    var two_table = $('#two_table').DataTable({
        "responsive": true,
        "language": {
            "decimal":        "",
            "emptyTable":     "<span style='text-align:center;width:100%;display:block;color:#000'>没有数据</span>",
            "info":           "从 第_START_ 到 _END_条 /共 _TOTAL_ 条数据",
            "infoEmpty":      "从 0到 0 /共 0条数据",
            "infoFiltered":   "(从 _MAX_ 条数据中检索)",
            "infoPostFix":    "",
            "thousands":      ",",
            "lengthMenu":     "每页显示 _MENU_ 条记录",
            "loadingRecords": "加载中...",
            "processing":     "搜索中...",
            "search":         "搜索:",
            "zeroRecords":    "<span style='text-align:center;width:100%;display:block;color:#000'>没有检索到数据</span>",
            "paginate": {
                "first":      "首页",
                "last":       "尾页",
                "previous": '<i class="demo-psi-arrow-left"></i>',
                "next": '<i class="demo-psi-arrow-right"></i>'
            },
        },
        "orderMulti": true,
        "processing": true,
        "serverSide": true,
        "lengthChange": true,
        "destroy": true,
        ajax: {
            url: 'projectInfoQuery',
            dataSrc: 'data',
            data: {
                'project_id' : project_id
            }
        },
        "columnDefs": [ {
            "targets": [0,1],
            "orderable": false
        } ],
        "columns": [
            { "data":"content"},
            { "data":"add_time"}
        ],
        "order": [ 0, 'desc' ],
        "lengthMenu": [ [ 50, 100, 200], [ 50, 100, 200] ],
        "dom": 't<".col-sm-4"i>'
    });
}